<?php
namespace app\api\job;

use app\api\model\App;
use app\api\service\NoticeService;
use app\api\model\Order as OrderModel;
use think\facade\Log;

class NoticeJob
{
    public function fire($job, $data)
    {
        if ($job->attempts() > 3) {
            Log::error('订单通知失败:\n' . json_encode($data));
            $job->delete();
            return ;
        }
        $order = OrderModel::find($data['id']);
        if (!$order) {
            Log::error('订单不存在', $data);
            $job->delete();
            return;
        }
        $app = App::where('appid',$order->appid)->find();
          if (!$app) {
            Log::error('应用不存在', ['appid' => $order->appid]);
            $job->delete();
            return;
        }
        $noticeService = new NoticeService();
    
        if($noticeService->send($order, $app)){
             Log::info('订单通知成功', ['order_id' => $order->id]);
            $job->delete();
            return;
        }
        $job->release(10);
    }
}